package cn.com.lyj6851.modules.shop.controller;

import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.aop.ForbidSubmit;
import cn.com.lyj6851.modules.shop.domain.YdyMaterial;
import cn.com.lyj6851.modules.shop.service.YdyMaterialService;
import cn.com.lyj6851.modules.shop.service.dto.YdyMaterialQueryCriteria;
import cn.com.lyj6851.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*
*/
@Api(tags = "商城:素材管理管理")
@RestController
@RequestMapping("/api/material")
public class MaterialController {

    private final YdyMaterialService yxMaterialService;

    public MaterialController(YdyMaterialService yxMaterialService) {
        this.yxMaterialService = yxMaterialService;
    }



    @GetMapping(value = "/page")
    @AopLog("查询素材管理")
    @ApiOperation("查询素材管理")
    public ResponseEntity<Object> getYdyMaterials(YdyMaterialQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxMaterialService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @AopLog("新增素材管理")
    @ApiOperation("新增素材管理")
    public ResponseEntity<Object> create(@Validated @RequestBody YdyMaterial resources){
        resources.setCreateId(SecurityUtils.getUsername());
        return new ResponseEntity<>(yxMaterialService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @AopLog("修改素材管理")
    @ApiOperation("修改素材管理")
    public ResponseEntity<Object> update(@Validated @RequestBody YdyMaterial resources){
        yxMaterialService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @AopLog("删除素材管理")
    @ApiOperation("删除素材管理")
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Object> deleteAll(@PathVariable String id) {
        yxMaterialService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }


}
